#ifndef _UART8_NB_H_
#define _UART8_NB_H_

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "at32f403a_407_gpio.h"
#include "at32f403a_407_usart.h"
#include "at32f403a_407_adc.h"
#include "at32f403a_407_crm.h"

/*******************UART8配置参数*************************/
#define GPIO_CLK CRM_GPIOE_PERIPH_CLOCK
#define UART_CLK CRM_UART8_PERIPH_CLOCK
#define UART_NUM UART8
#define GPIO_TX GPIOE
#define GPIO_RX GPIOE
#define PIN_TX GPIO_PINS_1
#define PIN_RX GPIO_PINS_0
#define UART_NUM_IRQn UART8_IRQn
#define UART_NUM_IRQ_FUN() UART8_IRQHandler()
#define RX_BUFFER_LEN_MAX 1024 // RX 缓存最大长度
#define recv_view 0 // 回显数据 开启：1

/*******************NB模块配置参数 - 华为云设备配置*************************/
#define HWY_BROKER_ADDRESS "5c5a18feb0.st1.iotda-device.cn-north-4.myhuaweicloud.com"
// 修正设备ID：从 686cd203d582f200183945b 改为 686cd203d582f2001839455b
#define HWY_CLIENTID "686cd203d582f2001839455b_DEVICE-XDW-001_0_0_2025070901"
#define HWY_PORT (8883)
#define HWY_USERNAME "686cd203d582f2001839455b_DEVICE-XDW-001"
#define HWY_DEVICEID "686cd203d582f2001839455b_DEVICE-XDW-001"
#define HWY_PASSWORD "12345678"  // 确认密码
#define HWY_PUBTOPIC "$oc/devices/686cd203d582f2001839455b_DEVICE-XDW-001/sys/properties/report"
#define HWY_SUBTOPIC "$oc/devices/686cd203d582f2001839455b_DEVICE-XDW-001/sys/properties/set"

typedef enum
{
    AT_ERROR = 0,
    AT_OK,
    AT_TIMEOUT
} AT_Rsp_Typedef;

/*******************UART API*******************************/
// 串口8初始化
void uart8_init(uint32_t bound);
// 发送1个byte数据
void USART8_send_byte(uint8_t byte);
// 发送多个byte数据
void USART8_send_data(uint8_t *data, uint32_t len);
// 发送字符串
void USART8_send_string(uint8_t *data);
// 清除串口接收数据
void USART8_clear_rx(void);
// 字符串转十六进制
void hex_to_hexStr(char *source, int sourceLen, char *dest);

/*******************NBIOT API*******************************/
// AT指令发送
int NB_Send_Cmd(char *cmd, char *ack, unsigned int waitms);
// 模块初始化
void nb_mqtt_init();
// 模块连接服务器
int nb_mqtt_conn(char *brokerAddress, uint16_t port, char *clientID, char *userName, char *password);
// 发布消息
int nb_mqtt_pub(char *topic, char *buf);
// 订阅消息
int nb_mqtt_sub(char *topic);

/*******************PM2.5 SENSOR API***********************/
// PM2.5传感器初始化
void pm25_init(void);
// 获取PM2.5数据
float pm25_get_data(void);

#endif